# 线性回归的sklearn调用实现
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import pandas as pd

# 读取数据
df = pd.read_excel('class/the house data2.xlsx')  # 确保文件路径正确
X=df[["the house square","from house to subway station "]].values
# X =df.drop(['房屋价格'],axis=1)#axis=0表示按行处理数据,axis=1表示按列处理数据
# y 是目标变量，即房屋价格
y = df['the house price'].values

# train_test_split,做了样本数据的打乱，划分数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1)#按0.1的比列的数据用于评估模型
# 准备模型  线性回归
lr =LinearRegression()
# 模型训练
lr.fit(X_train, y_train)
print(lr.coef_)
print(lr.intercept_)
y_pred = lr.predict(X_test)
# print(XX)
# 计算均方误差
mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error: {mse}")
#预测
y=lr.predict([[155,0.5]])
print(f'y:{y}')

